//
//  RotateView.m
//  XhlTrip
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RotateView.h"
#import "UIButton+WebCache.h"

@implementation RotateView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.rotateArr = [NSMutableArray array];
  
        }
    return self;
}

- (void)createScroller
{
    

    [self.rotateArr removeLastObject];
    [self.rotateArr removeObjectAtIndex:2];
    [self.rotateArr insertObject:[self.rotateArr lastObject] atIndex:0];
    [self.rotateArr insertObject:self.rotateArr[1] atIndex:self.rotateArr.count];
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height- 20 * Height)];
    _scrollerView.delegate = self;
    _scrollerView.contentSize = CGSizeMake(self.frame.size.width * self.rotateArr.count, 0);
    _scrollerView.pagingEnabled = YES;
    _scrollerView.contentOffset = CGPointMake(self.scrollerView.frame.size.width, 0);
    [self addSubview:_scrollerView];
    [_scrollerView release];
#pragma mark NStimer
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timer) userInfo:nil repeats:YES];

    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 30 * Width, self.frame.size.height - 20 * Height, 50 * Width, 20 * Height)];
    _page.numberOfPages = self.rotateArr.count - 2;
    _page.pageIndicatorTintColor = [UIColor whiteColor];
    _page.currentPageIndicatorTintColor = [UIColor grayColor];
    _page.currentPage = 0;
 
    [self addSubview:self.page];
    [_page release];
       
   
    
    for (int i = 0; i < [self.rotateArr count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tag = i + 1;

        [button addTarget:self action:@selector(nextpage:) forControlEvents:UIControlEventTouchUpInside];
        [button sd_setBackgroundImageWithURL:[self.rotateArr[i] objectForKey:@"image_url"]  forState:UIControlStateNormal];
    
        
        button.frame = CGRectMake(10 * Width + i * self.frame.size.width, 10 * Height, self.frame.size.width - 20 * Width, self.frame.size.height - 20 * Height);
        [self.scrollerView addSubview:button];
        
    }

   
}


- (void)nextpage:(UIButton *)button
{
    NSString *str = [NSString stringWithFormat:@"%@", [self.rotateArr[button.tag - 1] objectForKey:@"content"]];
    self.block(str);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
     _page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    // 最后一张照片就是第一张照片   所以改变偏移量
    if (scrollView.contentOffset.x == ([self.rotateArr count] - 1) * scrollView.frame.size.width) {
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
        _page.currentPage = 0;
    }
   
    // 第一张照片是最后一张照片所以偏移量改变
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake((([self.rotateArr count] - 2) * scrollView.frame.size.width), 0);
        _page.currentPage = 1;
    }
}

#pragma mark 定时器
- (void)timer {
    CGPoint point = CGPointMake(self.scrollerView.contentOffset.x + self.scrollerView.frame.size.width, 0);
    [self.scrollerView setContentOffset:point animated:YES];
    
    // 图片自动滚动后判断偏移量,如果是最后一张图就直接设置偏移量到第二张图.实现无限滚动
    if (self.scrollerView.contentOffset.x == ([self.rotateArr count] - 1) * self.scrollerView.frame.size.width) {
        self.scrollerView.contentOffset = CGPointMake(self.scrollerView.frame.size.width, 0);
    }
    // page与图片同步++-
    if (self.scrollerView.contentOffset.x == ([self.rotateArr count] - 2) * self.frame.size.width) {
        self.page.currentPage = 0;
    } else {
        self.page.currentPage = self.scrollerView.contentOffset.x / self.frame.size.width;
    }
}

- (void)dealloc
{
    Block_release(_block);
    [_rotateArr release];
    [_scrollerView release];
    [_page release];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
